package com.yqb.study.common.session.args;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.yqb.study.common.session.IRedisSession;
import com.yqb.study.common.session.RedisSessionContextHolder;

@Service
public class RedisSessionArgsResolver implements HandlerMethodArgumentResolver {

	public boolean supportsParameter(MethodParameter parameter) {
		// 获取参数类型
		Class<?> clazz = parameter.getParameterType();
		return clazz == IRedisSession.class;
	}

//	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
//			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//		// 取出request和response
//		HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
//
//		// 有的token从cookie中带入，有的是从request请求中带入
//		String token = CookieUtil.getToken(request, ICookieKey.TOKEN_KEY);
//		// 取到浏览器发送来的token，从第三方缓存中取到对应的session
//		IRedisSession sessionRedis = new RedisSessionImpl(token);
//
//		return sessionRedis;
//	}
	
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		// 从第三方缓存中取到对应的session
		IRedisSession sessionRedis = RedisSessionContextHolder.getThreadRedisSession();	

		return sessionRedis;
	}	
}
